Because I keep saying I will remember how do do things and then I don’t so I’m putting the bookmarked links and comments into one place to try and help me spend less time searching for answers.
Why do I always forget the direction of these?
hjust: 0 = left-aligned, 0.5=center, 1 = right-aligned
vjust: 0 = top-aligned, 0.5=middle, 1 = bottom-aligned
Sometimes I’m working on two different types of plots (like a bar chart and a scatter plot) that happen to have the same x-axis. I want to line up these axes so that when the plots are stacked the values correspond to the same date.
# two different bar charts
A <- ggplot(mpg, aes(class))+geom_bar()+coord_flip()+ylim(0, 109)
B <- ggplot(mpg, aes(drv))+geom_bar()+coord_flip()+ylim(0, 109)Using grid.arrange command from the gridExtra package does not line up axes.
Use grid.draw command from the grid package.
Source
#make plots into Grobs (grid graphical object)
gA <- ggplotGrob(A)
gB <- ggplotGrob(B)
grid::grid.draw(rbind(gA, gB))Another option is facet_wrap, which works great if the axes are the same for the different varaibles you want to compare, but isn’t a perfect solution to everything (see below).
tidy.df <- pivot_longer(mpg, c(class, drv))
ggplot(tidy.df, aes(value))+geom_bar()+facet_wrap(~name)Scatter plots and bar charts will not line up automatically, even when using the grid.draw command detailed above. This is because their default limits are different given that the bar chart is centered on the value and the scatter plot is a single point on the value.
#work with smaller subset of data from economics, part of ggplot2 package
startdate <- "2014-06-01"
economics_small <- economics %>%
filter(date >= as.Date(startdate)) %>%
arrange(date)A <- ggplot(economics_small, aes(date, unemploy))+
geom_bar(stat="identity")+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)
B <- ggplot(economics_small, aes(date, uempmed))+
geom_point()+geom_line()+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)
gA <- ggplotGrob(A)
gB <- ggplotGrob(B)
grid::grid.draw(rbind(gA, gB))In order to line the up there a a couple of options.
If you make the limit the first x-value, the bar chart will not show up (remember it’s centered over the value).
A <- ggplot(economics_small, aes(date, unemploy))+
geom_bar(stat="identity")+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)+
xlim(as.Date(startdate), NA)
B <- ggplot(economics_small, aes(date, uempmed))+
geom_point()+geom_line()+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)+
xlim(as.Date(startdate), NA)
gA <- ggplotGrob(A) ## Warning: Removed 1 rows containing missing values (geom_bar).
This can be fixed by adding a half unit to the x-axis (i.e. having the lower limit be half-unit lower than smallest x-value). In this case the unit is a month, so a half-unit would be ~15 days.
## Time difference of 15 days
A <- ggplot(economics_small, aes(date, unemploy))+
geom_bar(stat="identity")+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)+
xlim(as.Date(startdate)-HalfUnit, NA)
B <- ggplot(economics_small, aes(date, uempmed))+
geom_point()+geom_line()+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)+
xlim(as.Date(startdate)-HalfUnit, NA)
gA <- ggplotGrob(A)
gB <- ggplotGrob(B)
grid::grid.draw(rbind(gA, gB))Bar charts are automatically centered over the x-value. Bar charts (and any geom object) can be shifted by using position - position_nudge()). The shift needs to be half a unit on the x-axis, again here it is monthly data so a half unit would be ~15 days.
Source
A <- ggplot(economics_small, aes(date, unemploy))+
geom_bar(stat="identity", position = position_nudge(x = HalfUnit))+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)
B <- ggplot(economics_small, aes(date, uempmed))+
geom_point()+geom_line()+
geom_vline(xintercept = as.Date(startdate), color="red", size=2)
gA <- ggplotGrob(A)
gB <- ggplotGrob(B)
grid::grid.draw(rbind(gA, gB))test expression goes in parenthesis () and the statment goes in the curly brakets {}
if (test) { statment }
R is a bit finicky with where the brakets go; I get errors when I put else on a new line by itself - it wants to have the right braket before it; } else
if (test) {
statment #1
} else {
statment #2
}
if (test) {
statment #1
} elseif {
statment #2
} elseif {
statment #3
} else {
statment #4
}

Re sizing images in Markdown is required if you are knitting to a pdf - because you can’t use HTML code.
Tip and tricks for workign wtih images and figures in R Markdown documents - hollie@zevross.com
Adjust the out.width and out.height in the R chunk options
{r, out.width="50%"}
img <- "Peegs.jpg" #path to image
knitr::include_graphics(img) #in the knitr package
In my opinion, HTML is a lot easier to use for images options.
<img src="Peegs.jpg" alt="this is Daffodil and Blossom" width="50%">